package cz.spock.cms.service;

import java.util.List;

import org.springframework.stereotype.Service;

import cz.spock.cms.template.entity.TemplateSource;
import cz.spock.core.service.BaseService;

@Service
public class TemplateSourceService extends BaseService {
    
    
    public TemplateSource createTemplateSource(String name, String desc, String template) {
        if(name == null) {
            // TODO name null
            return null;
        }
        if(template == null) {
            // TODO
            return null;
        }
        // TODO check name uniqueness
        TemplateSource ts = new TemplateSource();
        ts.setDescription(desc);
        ts.setName(name);
        ts.setTemplate(template);
        ts.setId(dao.put(ts).getId());
        return ts;
    }
    
    public List<TemplateSource> getAllTemplateSources() {
        return dao.ofy().query(TemplateSource.class).list();
    }
    
    public TemplateSource getTemplateSourceById(Long id) {
        if(id == null) return null;
        return dao.find(TemplateSource.class, id);
    }
    
    /**
     * will be used somewhere?
     * 
     * @param name
     * @return
     */
    public TemplateSource findTemplateSourceByName(String name) {
        if(name == null) return null;
        return dao.ofy().query(TemplateSource.class).filter("name", name).get();
    }
    
    /**
     * searches for all pages/nodes which uses this template
     * @param id
     * @return
     */
    public List findReferences(Long id) {
        
        return null;
    }
    
    /**
     * deletes template source from db
     * @param id
     */
    public void deleteTemplate(Long id) {
        if(id == null) return;
        dao.delete(TemplateSource.class, id);
    }    
    
    /**
     * deletes template source from db
     * @param ts
     */
    public void deleteTemplate(TemplateSource ts) {
        if(ts == null) return;
        dao.delete(ts);
    }
    
    /**
     * creates nodes and fields model that can be used with given template
     * 
     * @param projectId - 
     * @param templateId
     * @param nodeId - top node for which the structure is created
     */
    public void createDataModelFromTemplate(Long projectId, Long templateId, Long nodeId) {
        
    }

}
